package yxy.game.pm2.module.camptower;

import yxy.apple.logger.Logger;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.HashMap;
import java.util.Map;

/**
 * 阵营塔-四象归一
 *
 * @ClassName FourSealModule
 * @Author jingtao-zheng
 * @Date 2022/8/9 14:32
 * @Version 1.0
 **/
public class FourSealModule extends PlayerModule {

    private Map<Integer, Integer> fourSeal = new HashMap<>();// 四象等级 key-阵营类型 value等级

    public Map<Integer, Integer> getFourSeal() {
        return fourSeal;
    }

    public void setFourSeal(Map<Integer, Integer> fourSeal) {
        this.fourSeal = fourSeal;
    }

    //===============================================================redis
    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            //check();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public FourSealModule get(String playerCode) {
        try {
            FourSealModule module = getv(key(playerCode));
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), FourSealModule.class);
                if (module != null) {
                    module.set();
                }//把数据同步到redis
            }
            if (module == null) {
                module = create(playerCode);
            }
            //module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static FourSealModule create(String playerCode) {
        FourSealModule module = new FourSealModule();
        module.setPlayerCode(playerCode);
        Map<Integer, Integer> fourSeal = module.getFourSeal();
        fourSeal.put(Constant.WEI, 0);
        fourSeal.put(Constant.SHU, 0);
        fourSeal.put(Constant.WU, 0);
        fourSeal.put(Constant.QUN, 0);
        module.set();
        return module;
    }
    protected static Logger logger = Logger.create(FourSealModule.class);

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            logger.info("四象归一保存玩家code:%s",playerCode);
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                FourSealModule fourSeal = getv(key(playerCode));
                if (fourSeal != null) MySQLCacheDAO.setV(key, fourSeal);
            }
        } catch (Exception e) {
            logger.error("四象归一出错保存玩家code:%s",playerCode);
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            logger.info("四象归一移除redis玩家code:%s",playerCode);
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            logger.error("四象归一出错redis玩家code:%s",playerCode);
            e.printStackTrace();
        }
    }

    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:FOURSEAL", playerCode);
    }
    @Override
    public String lockKey() {
        return null;
    }
}
